<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="1title；Sql性能调优-mysql慢查询  ​        mysql的慢查询：（SQL默认规则）只要超过规定的时间（默认10秒）没有返回结果的，都属于慢查询，存放在日志文件中。 一、数据库慢查询配置1.查看数据库的链接次数：show status like “connection” 2.查询MySQL是否启动慢查询记录日志off默认禁用：show variables like “slow">
<meta property="og:type" content="article">
<meta property="og:title" content="Sql性能调优-mysql慢查询">
<meta property="og:url" content="https://www.suichen.top/2020/04/21/Sql%E6%80%A7%E8%83%BD%E8%B0%83%E4%BC%98-mysql%E6%85%A2%E6%9F%A5%E8%AF%A2/index.html">
<meta property="og:site_name" content="随尘的博客">
<meta property="og:description" content="1title；Sql性能调优-mysql慢查询  ​        mysql的慢查询：（SQL默认规则）只要超过规定的时间（默认10秒）没有返回结果的，都属于慢查询，存放在日志文件中。 一、数据库慢查询配置1.查看数据库的链接次数：show status like “connection” 2.查询MySQL是否启动慢查询记录日志off默认禁用：show variables like “slow">
<meta property="article:published_time" content="2020-04-21T02:43:22.983Z">
<meta property="article:modified_time" content="2020-04-21T02:43:22.985Z">
<meta property="article:author" content="suichen">
<meta name="twitter:card" content="summary">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://www.suichen.top/2020/04/21/Sql性能调优-mysql慢查询/"/>





  <title>Sql性能调优-mysql慢查询 | 随尘的博客</title>
  








<meta name="generator" content="Hexo 4.2.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">随尘的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/%20" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://www.suichen.top/2020/04/21/Sql%E6%80%A7%E8%83%BD%E8%B0%83%E4%BC%98-mysql%E6%85%A2%E6%9F%A5%E8%AF%A2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="suichen">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="随尘的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Sql性能调优-mysql慢查询</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-04-21T10:43:22+08:00">
                2020-04-21
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">title；Sql性能调优-mysql慢查询</span><br></pre></td></tr></table></figure>

<p>​        mysql的慢查询：（SQL默认规则）只要超过规定的时间（默认10秒）没有返回结果的，都属于慢查询，存放在日志文件中。</p>
<h5 id="一、数据库慢查询配置"><a href="#一、数据库慢查询配置" class="headerlink" title="一、数据库慢查询配置"></a>一、数据库慢查询配置</h5><p>1.查看数据库的链接次数：show status like “connection”</p>
<p>2.查询MySQL是否启动慢查询记录日志off默认禁用：show variables like “slow_query_log”</p>
<p>3.设置慢查询 a)非永久性：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">set global slow_query_log&#x3D;&quot;on&quot;;&#x2F;&#x2F;设置启动成功后会在本地(C:&#x2F;ProgramData&#x2F;MySQL&#x2F;MySQL Server5.6&#x2F;Data&#x2F;)新增一个日志文件DESKTOP_KD9H54U-slow.log   注意：是ProgramData目录下</span><br><span class="line">set long_query_time&#x3D;1;&#x2F;&#x2F;默认10秒</span><br></pre></td></tr></table></figure>

<p>   b)永久性：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">打开my.ini文件&#x2F;&#x2F;也是ProgramData文件下，该文件是一个隐藏文件。</span><br><span class="line">[mysqld]下加入配置：</span><br><span class="line">slow_query_log&#x3D;on</span><br><span class="line">long_query_time&#x3D;1</span><br></pre></td></tr></table></figure>

<p>设置完成之后不要忘了重启MySQL</p>
<p>4.查询慢查询设置时间：show variables like “long_query_time”</p>
<p>5.查询慢查询的次数以及sql：show status like “slow_queries”</p>
<p>6.去配置的datadir路径查看慢查询记录。</p>
<h5 id="二、分析查询日志"><a href="#二、分析查询日志" class="headerlink" title="二、分析查询日志"></a>二、分析查询日志</h5><p>直接分析MySQL慢查询日志，利用explain关键字可以模拟优化器执行SQL查询语句来分析啥情况慢查询语句，如：执行explain select * from user order fied_time limt 0,1000</p>
<p>得到如下结果：显示结果分析：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">table|type|possible_keys|key|key</span><br><span class="line">table 显示这一行的数据是关于哪张表的</span><br><span class="line">type  这是重要的列，显示连接使用了何种类型，从最好到最差的链接类型为const\q_reg\ef\ange\indexhe和All</span><br><span class="line">rows 显示需要扫描行数</span><br><span class="line">key  使用索引</span><br></pre></td></tr></table></figure>



<h5 id="三、常用的慢查询优化"><a href="#三、常用的慢查询优化" class="headerlink" title="三、常用的慢查询优化"></a>三、常用的慢查询优化</h5><p>1）索引没起作用的情况</p>
<p>a)使用了like关键字：查询语句中匹配字符串的第一个字符为“%”，索引不会起作用。只有“%”不在第一个位置索引才会起作用。</p>
<p>b)使用多列索引的查询语句：MYSQL可以为多个字段创建索引。一个索引最多可以包含16个字段，对于多列索引，只有查询条件使用了这些字段中的第一个字段时，索引才会起作用。</p>
<p>2）优化数据库结构</p>
<p>分表：表中一行的数据过多时可以把使用频率少的数据分出来</p>
<p>中间表：可以采用中间表的形式把表关联起来</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/04/19/tomcat%E4%BC%98%E5%8C%96/" rel="next" title="tomcat优化">
                <i class="fa fa-chevron-left"></i> tomcat优化
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/04/22/%E6%8B%A6%E6%88%AA%E5%99%A8%E4%B8%8E%E8%BF%87%E6%BB%A4%E5%99%A8/" rel="prev" title="拦截器与过滤器">
                拦截器与过滤器 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">suichen</p>
              <p class="site-description motion-element" itemprop="description">记录成长</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/%20%7C%7C%20archive">
              
                  <span class="site-state-item-count">14</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-5"><a class="nav-link" href="#一、数据库慢查询配置"><span class="nav-number">1.</span> <span class="nav-text">一、数据库慢查询配置</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#二、分析查询日志"><span class="nav-number">2.</span> <span class="nav-text">二、分析查询日志</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#三、常用的慢查询优化"><span class="nav-number">3.</span> <span class="nav-text">三、常用的慢查询优化</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">suichen</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
